package com.hanqian.kepler.core.service.core.impl;

import com.hanqian.kepler.common.base.dao.BaseDao;
import com.hanqian.kepler.common.base.service.BaseServiceImpl;
import com.hanqian.kepler.common.enums.BaseEnumManager;
import com.hanqian.kepler.common.jpa.specification.Rule;
import com.hanqian.kepler.common.jpa.specification.SpecificationFactory;
import com.hanqian.kepler.core.dao.primary.core.TargetDescEvaluateDao;
import com.hanqian.kepler.core.entity.primary.core.TargetDesc;
import com.hanqian.kepler.core.entity.primary.core.TargetDescEvaluate;
import com.hanqian.kepler.core.service.core.TargetDescEvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * newFile
 * ============================================================================
 * author : dzw
 * createDate:  2021/1/6 。
 * ============================================================================
 */
@Service
public class TargetDescEvaluateServiceImpl extends BaseServiceImpl<TargetDescEvaluate, String> implements TargetDescEvaluateService {

	@Autowired
	private TargetDescEvaluateDao targetDescEvaluateDao;

	@Override
	public BaseDao<TargetDescEvaluate, String> getBaseDao() {
		return targetDescEvaluateDao;
	}

	@Override
	public Page<TargetDescEvaluate> findEvaluatePageByTargetDesc(TargetDesc targetDesc, Integer v, Pageable pageable, List<Rule> rules) {
		if(targetDesc == null || targetDesc.getVersionIndex() == null) return null;
		if(rules == null) rules = new ArrayList<>();
		rules.add(Rule.eq("state", BaseEnumManager.StateEnum.Enable));
		rules.add(Rule.eq("targetDesc.id", targetDesc.getId()));
		rules.add(Rule.eq("versionIndex", v!=null ? v :targetDesc.getVersionIndex()));
		return findAll(SpecificationFactory.where(rules), pageable);
	}

	@Override
	public Long findEnableCount() {
		return targetDescEvaluateDao.findEnableCount();
	}
}
